#include "CSearchCtrl.h"

CSearchCtrl::CSearchCtrl(QWidget *parent)
{
    m_pBtnSearch = nullptr;//搜索
    m_pEdit = nullptr;//输入框
    Init();
}
CSearchCtrl::~CSearchCtrl()
{
    if(m_pBtnSearch)
    {
        delete m_pBtnSearch;
        m_pBtnSearch = nullptr;
    }
    if(m_pEdit)
    {
        delete m_pEdit;
        m_pEdit = nullptr;
    }
}

void CSearchCtrl::Init()
{
    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->setSpacing(5); //间距
    mainLayout->setContentsMargins(0,0,0,0);

    m_pBtnSearch = new QPushButton();
    m_pEdit = new QLineEdit();
   // m_pBtnSearch->setFixedSize(100, 50);
    m_pEdit->setMaximumHeight(36);
    m_pBtnSearch->setFixedSize(36, 36);
    //m_pBtnSearch->setText("搜索");

    m_pBtnSearch->setIcon(QIcon(":/images/search.png"));

    m_pBtnSearch->setStyleSheet(
        R"(
            QPushButton {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );

    m_pEdit->setStyleSheet(
        R"(
            QLineEdit {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );


    mainLayout->addWidget(m_pEdit);
    mainLayout->addWidget(m_pBtnSearch);

    connect(m_pEdit, &QLineEdit::returnPressed, this,&CSearchCtrl::OnSearch);
    connect(m_pBtnSearch, &QPushButton::clicked, this,&CSearchCtrl::OnSearch);
}

void CSearchCtrl::OnSearch()
{
    //获取输入框内容，发送信号
    if(!m_pEdit)
    {
        return;
    }


    emit SearchClicked(m_pEdit->text());

}

void CSearchCtrl::GetSearchText(QString & strText)
{
    if(!m_pEdit)
    {
        return;
    }

    strText = m_pEdit->text();
}
